package com.aishua.andpaydemo.fragment;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.aishua.andpaydemo.Constant;
import com.aishua.andpaydemo.R;
import com.aishua.andpaydemo.adapter.CaifuAdapter;
import com.aishua.andpaydemo.fragment.caifufragment.BaoxianFragment;
import com.aishua.andpaydemo.fragment.caifufragment.JiekuanFragment;
import com.aishua.andpaydemo.fragment.caifufragment.LicaiFragment;
import com.aishua.andpaydemo.utils.TableLayoutChangeColorUtils;

import java.util.ArrayList;

/**
 * Created by aishua on 2018/5/15.
 */

public class FoundFragment extends BaseFragment implements View.OnClickListener {

    private TabLayout tab_caifu;
    private ViewPager vp_caifu_content;
    private ArrayList<BaseFragment> baseFragments;
    private ArrayList<String> list_title;
    View view;
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

         view = View.inflate(getContext(), R.layout.fragment_found, null);

        return view;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onResume() {
        super.onResume();
        initView(view);

    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void initView(View view) {

        tab_caifu = view.findViewById(R.id.tab_caifu);
        vp_caifu_content = view.findViewById(R.id.vp_caifu_content);
        int clolrID = TableLayoutChangeColorUtils.getThemeColorId(getContext());
        tab_caifu.setBackground(getContext().getDrawable(clolrID));
        //将名称加载tab名字列表，正常情况下，我们应该在values/arrays.xml中进行定义然后调用
        list_title = new ArrayList<>();
        list_title.add("理财");
        list_title.add("借款");
        list_title.add("保险");

        //设置TabLayout的模式
        tab_caifu.setTabMode(TabLayout.MODE_FIXED);
        //为TabLayout添加tab名称
//        tab_caifu.addTab(tab_caifu.newTab().setText(list_title.get(0)));
//        tab_caifu.addTab(tab_caifu.newTab().setText(list_title.get(1)));
//        tab_caifu.addTab(tab_caifu.newTab().setText(list_title.get(2)));

        initFragment();
        vp_caifu_content.setAdapter(new CaifuAdapter(getChildFragmentManager(),baseFragments,list_title));
        if(Constant.isThrid){
            vp_caifu_content.setCurrentItem(1);
            Constant.isThrid = false;
        }else {
            vp_caifu_content.setCurrentItem(0);
        }
        tab_caifu.setupWithViewPager(vp_caifu_content);
    }
    private LicaiFragment licaiFragment;
    private JiekuanFragment jiekuanFragment;
    private BaoxianFragment baoxianFragment;
    public void initFragment() {

        baseFragments = new ArrayList<>();
        baseFragments.clear();

        licaiFragment  = new LicaiFragment();
        jiekuanFragment = new JiekuanFragment();
        baoxianFragment = new BaoxianFragment();

        baseFragments.add(licaiFragment);
        baseFragments.add(jiekuanFragment);
        baseFragments.add(baoxianFragment);
    }
    @Override
    public void onClick(View v) {
    }

}
